<?php

class PasswordController extends Controller {

	public function actionRestore() {
		$this->pageTitle = 'Восстановление пароля';

		if (isset($_GET['hash'])) {
			$c = new CDbCriteria();
			$c->compare('"userHash"', $_GET['hash']);
			$user = Users::model()->find($c);
			if ($user === NULL)
				throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
			$model = new RestoreForm();

//			if (isset($_GET['email']))
//				$model->userEmail = $_GET['email'];
			if (isset($_POST['RestoreForm'])) {
				$model->attributes = $_POST['RestoreForm'];

				if ($model->validate()) {
					$user->userPassword = md5($user->userEmail . $model->userPassword);
					$user->userHash = 0;
					if ($user->save(false)) {
						Yii::app()->user->setFlash('success', ['Поздравляем!', 'Ваш пароль успешно изменен!']);
						$this->redirect(Yii::app()->homeUrl);
					}
				}
			}
			$this->render('restore', array('model' => $model));
		} else
			throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
	}

}
